package com.ruoyi.points.service.impl;

import java.util.List;

import com.ruoyi.points.domain.QizhongTest;
import com.ruoyi.points.mapper.QizhongTestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.points.mapper.QizhongUserMapper;
import com.ruoyi.points.domain.QizhongUser;
import com.ruoyi.points.service.IQizhongUserService;

/**
 * 用户余额Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-06
 */
@Service
public class QizhongUserServiceImpl implements IQizhongUserService
{
    @Autowired
    private QizhongUserMapper qizhongUserMapper;

    @Autowired
    QizhongTestMapper testMapper;

    /**
     * 查询用户余额
     *
     * @param id 用户余额主键
     * @return 用户余额
     */
    @Override
    public QizhongUser selectQizhongUserById(Long id)
    {
        return qizhongUserMapper.selectQizhongUserById(id);
    }

    /**
     * 查询用户余额列表
     *
     * @param qizhongUser 用户余额
     * @return 用户余额
     */
    @Override
    public List<QizhongUser> selectQizhongUserList(QizhongUser qizhongUser)
    {
        return qizhongUserMapper.selectQizhongUserList(qizhongUser);
    }

    /**
     * 新增用户余额
     *
     * @param qizhongUser 用户余额
     * @return 结果
     */
    @Override
    public int insertQizhongUser(QizhongUser qizhongUser)
    {
        return qizhongUserMapper.insertQizhongUser(qizhongUser);
    }

    /**
     * 修改用户余额
     *
     * @param qizhongUser 用户余额
     * @return 结果
     */
    @Override
    public int updateQizhongUser(QizhongUser qizhongUser)
    {
        System.out.println(qizhongUser.getMoney());
        int i = qizhongUserMapper.updateMoney(qizhongUser);
      QizhongUser money=   qizhongUserMapper.selectQizhongUserBySfz(qizhongUser.getSfz());
      if(money.getMoney()<qizhongUser.getMoney()){
//          失败了不扣钱
//          qizhongUserMapper.upMoney(qizhongUser);
          return 1;
      };
      if(money.getMoney()>qizhongUser.getMoney()){
          QizhongTest qizhongTest1=  qizhongUserMapper.selectQizhongUserByName(qizhongUser.getName());
//          成功了减库存
          testMapper.updateTestKucun(qizhongTest1.getId());
//          成功了减钱
          qizhongUserMapper.updateMoney(qizhongUser);
          return 0;
      }
      return 3;
    }

    /**
     * 批量删除用户余额
     *
     * @param ids 需要删除的用户余额主键
     * @return 结果
     */
    @Override
    public int deleteQizhongUserByIds(Long[] ids)
    {
        return qizhongUserMapper.deleteQizhongUserByIds(ids);
    }

    /**
     * 删除用户余额信息
     *
     * @param id 用户余额主键
     * @return 结果
     */
    @Override
    public int deleteQizhongUserById(Long id)
    {
        return qizhongUserMapper.deleteQizhongUserById(id);
    }

    @Override
    public int sendMoney(QizhongUser qizhongUser) {
        return qizhongUserMapper.sendMoney(qizhongUser);
    }
}
